<template>
  <div>
    <el-dialog v-bind="$attrs" v-on="$listeners" @open="onOpen" @close="onClose" title="创建班级">
      <el-form
        ref="elForm"
        :model="formData"
        :rules="rules"
        size="medium"
        label-width="100px"
        label-position="left"
      >
        <el-form-item label="班级名称" prop="name">
          <el-input
            v-model="formData.name"
            placeholder="请输入班级名称"
            clearable
            :style="{width: '100%'}"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handelConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { createClazz } from "../../../../api/course";

export default {
  inheritAttrs: false,
  components: {},
  props: ["courseId"],
  data() {
    return {
      formData: {
        name: undefined,
        cno:""
      },
      rules: {
        name: [
          {
            required: true,
            message: "请输入班级名称",
            trigger: "blur"
          }
        ]
      }
    };
  },
  computed: {
  },
  methods: {
    onOpen() {},
    onClose() {
      this.$refs["elForm"].resetFields();
    },
    close() {
      this.$emit("update:visible", false);
    },
    async post() {
      this.formData.cno = this.courseId
      const { data: res } = await createClazz(this.formData);
      if (res.code == 20000) {
        this.$parent.getList(this.courseId);
        this.$notify.success({
          title: res.message,
          message: "班级创建成功"
        });
      } else {
        this.$message.error("班级创建失败：" + res.message);
      }
    },
    handelConfirm() {
      this.$refs["elForm"].validate(valid => {
        if (!valid) return;
        this.post();
        this.close();
      });
    }
  }
};
</script>
<style>
</style>
